package com.itlong.cloud.rabbitmq.config;

import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.utils.rabbitMQ.BaseConfig;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      极光推送服务消息队列配置
 * </desc>
 *
 * @createDate 2017-11-30
 */
@Configuration
public class RabbitConfig extends BaseConfig{

    //声明队列，true表示持久化该队列
    @Bean
    public Queue houseHoldJPushRabbit() {
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_HOUSEHOLD_JPUSH_QUEUE, true, false, false, map);
        return queue;
    }

    //项目状态更改队列
    @Bean
    public Queue projectStatusJPushRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PROJECT_STATUS_QUEUE, true, false, false, map);
        return queue;
    }

    //账单状态变更队列
    @Bean
    public Queue paymentBillJPushRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PAYMENT_BILL_JPUSH_QUEUE, true, false, false, map);
        return queue;
    }

    //项目蓝牙电梯更改队列
    @Bean
    public Queue projectBluetoothElevatorJPushRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PROJECT_BLUETOOTH_ELEVATOR_QUEUE, true, false, false, map);
        return queue;
    }

    //声明交互器
    @Bean
    TopicExchange topicExchange() {return new TopicExchange(RabbitMQConstant.RABBITMQ_HOUSEHOLD_JPUSH_EXCHANGE);}

    //绑定
    @Bean
    public Binding houseHoldJPushbinding() {
        return BindingBuilder.bind(houseHoldJPushRabbit()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_HOUSEHOLD_JPUSH_KEY);
    }

    //绑定
    @Bean
    public Binding paymentBillJPushbinding() {
        return BindingBuilder.bind(paymentBillJPushRabbit()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_PAYMENT_BILL_JPUSH_KEY);
    }
    @Bean
    public Binding projectStatusJPushBinding(){
        return BindingBuilder.bind(projectStatusJPushRabbit()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_PROJECT_STATUS_KEY);
    }

    @Bean
    public Binding projectBluetoothElevatorJPushBinding(){
        return BindingBuilder.bind(projectBluetoothElevatorJPushRabbit()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_PROJECT_BLUETOOTH_ELEVATOR_KEY);
    }



    //云电梯产品型号队列
    @Bean
    public Queue productRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PRODUCT_QUEUE, true, false, false, map);
        return queue;
    }

    @Bean
    TopicExchange productTopicExchange() {return new TopicExchange(RabbitMQConstant.RABBITMQ_PRODUCT_EXCHANGE);}

    //绑定
    @Bean
    public Binding productBinding() {
        return BindingBuilder.bind(productRabbit()).to(productTopicExchange()).with(RabbitMQConstant.RABBITMQ_PRODUCT_KEY);
    }

    //云电梯设备队列
    @Bean
    public Queue DeviceElevatorRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_QUEUE, true, false, false, map);
        return queue;
    }

    @Bean
    TopicExchange deviceElevatorTopicExchange() {return new TopicExchange(RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_EXCHANGE);}

    //绑定
    @Bean
    public Binding deviceElevatorBinding() {
        return BindingBuilder.bind(DeviceElevatorRabbit()).to(deviceElevatorTopicExchange()).with(RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_KEY);
    }
    //Iot topice发布队列交互器
    @Bean
    TopicExchange iotPubTopicExchange() {return new TopicExchange(RabbitMQConstant.RABBITMQ_IOT_PUB_EXCHANGE);}

    //Iot topice发布队列
    @Bean
    public Queue iotPubRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_IOT_PUB_QUEUE, true, false, false, map);
        return queue;
    }
    //绑定
    @Bean
    public Binding iotPubBinding() {
        return BindingBuilder.bind(iotPubRabbit()).to(iotPubTopicExchange()).with(RabbitMQConstant.RABBITMQ_IOT_PUB_KEY);
    }

    //Iot topice发布队列
    @Bean
    public Queue HuaweiIotCallRabbit(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL_BY_HUAWEI);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_HUAW_CALL_IOT_PUB_QUEUE, true, false, false, map);
        return queue;
    }
    //绑定
    @Bean
    public Binding HuaweiIotCallBinding() {
        return BindingBuilder.bind(HuaweiIotCallRabbit()).to(iotPubTopicExchange()).with(RabbitMQConstant.RABBITMQ_HUAW_CALL_IOT_PUB_KEY);
    }
}
